package ALBasicServer.ALProcess;

import ALBasicCommon.ALBasicCommonFun;
import ALServerLog.ALServerLog;

/****************************
 * 多个步骤需要处理的时候通过本处理对象进行的过程控制处理
 * 
 * ALResDelegateProcess 带入一个可返回结果的回调处理，通过带入回调的参数，可以控制处理过程是否成功。失败的过程会调用monitor的onProcessFailStop
 * @author mj
 *
 */
public class ALResDelegateProcess extends _AALProcess
{
    /// <summary>
    /// 创建一个多过程过程对象
    /// </summary>
    /// <returns></returns>
    public static ALResDelegateProcess CreateResDelegateProcess(String _processTag) { return new ALResDelegateProcess(_processTag); }

    /// <summary>
    /// 设置处理操作
    /// </summary>
    private _IALProcessResDelegate _m_lDealAction;

    //失败回调
    private _IALProcessAction _m_dFailAction;
    //是否失败的时候也执行完成函数
    private boolean _m_bIsFailContinue;

    protected ALResDelegateProcess(String _processTag)
    {
    	super(_processTag);
    	
        _m_lDealAction = null;

        _m_dFailAction = null;
        _m_bIsFailContinue = false;
    }

    /// <summary>
    /// 添加一个执行过程
    /// </summary>
    /// <param name="_process"></param>
    /// <returns></returns>
    public ALResDelegateProcess setProcess(_IALProcessResDelegate _delegate)
    {
    	return setProcess(_delegate, null, true);
    }
    public ALResDelegateProcess setProcess(_IALProcessResDelegate _delegate, _IALProcessAction _failDelegate, boolean _isFailContinue)
    {
        if(null == _delegate)
            return this;

        if(isRunning())
        {
            ALServerLog.Error("add process when process started!");
            return this;
        }

        //设置处理
        _m_lDealAction = _delegate;
        _m_dFailAction = _failDelegate;
        _m_bIsFailContinue = _isFailContinue;

        return this;
    }

    /// <summary>
    /// 获取出来可以在监控对象外围补充输出的信息
    /// </summary>
    @Override
    protected String _processMonitorExInfo()
    {
        return "";
    }

    /// <summary>
    /// 进行处理
    /// </summary>
    @Override
    protected void _dealProcess(_IALProcessMonitor _monitor)
    {
        //无数据则直接完成
        if(null == _m_lDealAction)
        {
            _onProcessDone();
            return;
        }

        //处理过程
        try
        {
        	final _IALProcessMonitor tmpMonitor = _monitor;
        	final _AALProcess curProcess = this;
            _m_lDealAction.dealAction(new _ITALProcessAction<Boolean>()
		            {
		            	public void dealAction(Boolean _res)
		            	{
			            	if(!_res)
			            	{
			                    //执行失败函数
			                    if (null != _m_dFailAction)
			                        _m_dFailAction.dealAction();
			                    
			            		//设置进程被中断
			                	_m_cController._enable = false;
			
			                	//调用监控对象的失败处理函数
			                	if(null != tmpMonitor)
			                		tmpMonitor.onProcessFailStop(curProcess);
			            	}
			            	
			            	//继续设置进程完结
			            	_onProcessDone();
		            	}
		            });
        }
        catch (Exception _ex)
        {
            //调用回调
            if (null != _monitor)
            {
                _monitor.onErr(ALBasicCommonFun.getNowTimeMS() - processStartTimeMS(), getFullProcessTag(), _processMonitorExInfo(), _ex);
            }
            else
            {
                ALServerLog.Error("Delegate Process " + getFullProcessTag() + " Fail! " + _processMonitorExInfo());
                ALServerLog.Error(_ex.getMessage());
            }

            //执行失败函数
            if (null != _m_dFailAction)
                _m_dFailAction.dealAction();

            //判断失败是否需要执行成功函数
            if (!_m_bIsFailContinue)
        		//设置进程被中断
            	_m_cController._enable = false;

        	//调用监控对象的失败处理函数
        	if(null != _monitor)
        		_monitor.onProcessFailStop(this);

            //执行成功函数
            _onProcessDone();
        }
    }

    /// <summary>
    /// 当子处理对象完成时的处理
    /// </summary>
    @Override
    protected void _onChildDone()
    {
    }

    /// <summary>
    /// 重置处理
    /// </summary>
    @Override
    protected void _onDiscard()
    {
        _m_lDealAction = null;

        _m_dFailAction = null;
        _m_bIsFailContinue = false;
    }

    /// <summary>
    /// 完成的处理，需要放回缓存池
    /// </summary>
    @Override
    protected void _onDone()
    {
    }
}
